//
//  WYCRepaymentController.m
//  信用卡管理
//
//  Created by 中企互联 on 2017/11/14.
//  Copyright © 2017年 李胜军. All rights reserved.
//

#import "WYCIntelligentMoneyController.h"
#import "MyTextField.h"
#import "WYCSeeBillModel.h"
#import "LDCalendarView.h"
#import "NSDate+extend.h"
#import "WYCRepayModel.h"
#import "WYCTRepayCell.h"
#import "WYCSelectionPlanController.h"
#import "WYCPreviewController.h"

@interface WYCIntelligentMoneyController ()<UITableViewDelegate,UITableViewDataSource,WYCSelectionPlanDelegate>
{
    
    int summer;
    
    
    UILabel * backLabel;
    
    
    double string;
}

@property (nonatomic, strong)LDCalendarView *calendarView;//日历控件
@property (nonatomic, strong)NSMutableArray *seletedDays;//选择的日期

/** 模型二*/
@property (strong , nonatomic)WYCRepayModel * repayModel2;

/** label*/
@property (strong , nonatomic) UILabel *typelabel;

@property(nonatomic,strong)CALayer *bottomLineLayer;

@property (nonatomic, copy)NSString *showStr;

/** planID*/
@property(nonatomic,strong)NSString * planID;

/** 时间*/
@property(nonatomic,strong)UILabel * timeLbel;

/** 模型赋值*/
@property(nonatomic,strong)WYCSeeBillModel * seeBillModel;


@property(nonatomic,strong)UITableView * tableView;

/** cell1 金额输入框*/
@property(nonatomic,strong)UITextField *repaymentTextField;


/** cell2 余额输入框*/
@property(nonatomic,strong)UITextField *balanceTextField;


/** MyTextField*/
@property(nonatomic,strong)MyTextField *repayTextField;


@property (nonatomic,strong) NSMutableArray *repayarray;


/** 日期*/
@property(nonatomic,strong)UILabel *daylabel;

/** UILabel * backLabel*/
@property(nonatomic,strong)UILabel * backLabel;

/** 按钮*/
@property(nonatomic,strong)UIButton * selectButton;

@end

@implementation WYCIntelligentMoneyController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    //Nav
    [self setupNav];
    
    //请求
    [self setupRequster];
    
    self.repayarray = [NSMutableArray new];
    WYCRepayModel * repayModel = [[WYCRepayModel alloc]init];
    repayModel.title = @"还款金额:";
    repayModel.isenland = NO;
    repayModel.titleFeild = @"请输入还款金额";
    [self.repayarray addObject:repayModel];
    
    
    self.repayModel2 = [[WYCRepayModel alloc]init];
    self.repayModel2.title = @"卡内余额:";
    self.repayModel2.isenland = NO;
    self.repayModel2.titleFeild = @"请输入卡内余额";
    [self.repayarray addObject:self.repayModel2];
    [self.repayModel2 addObserver:self forKeyPath:@"coment" options:NSKeyValueObservingOptionNew context:nil];
    
    WYCRepayModel * repayModel3 = [[WYCRepayModel alloc]init];
    repayModel3.title = @"还款天数：";
    repayModel3.isenland = YES;
    [self.repayarray addObject:repayModel3];
    
    
    WYCRepayModel * repayModel4 = [[WYCRepayModel alloc]init];
    repayModel4.title = @"消费日期:";
    repayModel4.titleFeild = @"选择还款日期";
    repayModel4.isenland = YES;
    [self.repayarray addObject:repayModel4];
    
    
    WYCRepayModel * repayModel5 = [[WYCRepayModel alloc]init];
    repayModel5.title = @"消费计划:";
    repayModel5.titleFeild = @"选择消费计划";
    repayModel5.isenland = YES;
    [self.repayarray addObject:repayModel5];
    
    
    
    
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(test) name:@"TextField" object:nil];
    
    WYCSelectionPlanController * selection = [[WYCSelectionPlanController alloc] init];
    selection.Delegate = self;
    
    //头部界面创建
    //    [self setupCreat];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    WYCLog(@"-----------");
    WYCRepayModel *model1 = self.repayarray[0];
    
    WYCRepayModel *model2 = self.repayarray[1];
    
    WYCRepayModel * model3 = self.repayarray[2];
    
    WYCRepayModel * model4 = self.repayarray[3];
    
    
    int balanceintValue = [model2.coment intValue];
    
    int huankuan = [model1.coment intValue];
    
    int onestr = huankuan * 0.06;
    
    int str = balanceintValue;
    
    
    if (str < onestr) {
        
        [self showToast:@"余额不能低于还款金额的6%"];
        return;
    }
}
- (void)test{
    
    WYCRepayModel *model1 = self.repayarray[0];
    WYCRepayModel *model2 = self.repayarray[1];
    WYCRepayModel *model3 = self.repayarray[2];
    
    int repaymentintValue = [model1.coment intValue];
    
    int balanceintValue = [model2.coment intValue];
    
    if (repaymentintValue == 0 || balanceintValue == 0) {
        model3.coment = @"";
    }else{
        
        
        WYCRepayModel *astr = self.repayarray[0];
        double str = [astr.coment doubleValue];
        double str2 = [[WYCAccountTool unarchiverepaymentRate] doubleValue];
        
        string = (str * str2) / 100;
        
//        backLabel.text = [NSString stringWithFormat:@"手续费=还款总金额×0.69%+还款笔数×1元"];
        double  dascount = repaymentintValue / balanceintValue - string;
        
        NSString * stringDday = [NSString stringWithFormat:@"%.2f",dascount];
        
        NSArray * dayarray = [stringDday componentsSeparatedByString:@"."];
        if (dayarray[1] != 0) {
            double sm = [dayarray[0] intValue] + 1;
            double i =  (balanceintValue - string - sm) * 1;
            double n = repaymentintValue / i;
            NSString * nStr = [NSString stringWithFormat:@"%.2f",n];
            NSArray * narr = [nStr componentsSeparatedByString:@"."];
            
            if (narr[1] != 0) {
                summer = [narr[0] intValue] + 1;
                model3.coment = [NSString stringWithFormat:@"%d",summer];
                WYCLog(@"不是0model3是%@",model3.coment);
            }
            
        }else{
            summer = [dayarray[0] intValue];
            model3.coment = [NSString stringWithFormat:@"%d",summer];
            WYCLog(@"是0model3是%@",model3.coment);
            
        }
        
        //        model3.coment = [NSString stringWithFormat:@"%d",summer];
        
    }
    
    
    WYCRepayModel *astr = self.repayarray[0];
    double str = [astr.coment doubleValue];
    double str2 = [[WYCAccountTool unarchiverepaymentRate] doubleValue];
    
    string = str * str2 / 100;
    NSString * string = @"%";
    backLabel.text = [NSString stringWithFormat:@"手续费=还款总金额×0.69%@+还款笔数×1元",string];
}

- (NSString *)showStr {
    NSMutableString *str = [NSMutableString string];
    
    [str appendString:@""];
    //从小到大排序
    [self.seletedDays sortUsingComparator:^NSComparisonResult(NSNumber *obj1, NSNumber *obj2) {
        return [obj1 compare:obj2];
    }];
    for (NSNumber *interval in self.seletedDays) {
        NSString *partStr = [NSDate stringWithTimestamp:interval.doubleValue/1000.0 format:@"yyyy-MM-dd"];
        [str appendFormat:@"%@,",partStr];
        [self.tableView reloadData];
    }
    return [str copy];
}

#pragma make -------->>请求数据
- (void)setupRequster{
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"cardId"] = self.cardId;
    params[@"token"] = [WYCAccountTool unarchivetoken];
    
    [HttpTool POST:creditcarddetails params:params success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([[responseObject objectForKey:@"code"] intValue] == 0) {
            NSDictionary * dict = responseObject[@"result"];
            self.seeBillModel = [WYCSeeBillModel mj_objectWithKeyValues:dict];
        }else{
            
            [self showToast:msgStr];
        }
        
        //界面
        [self setupCreat];
    } fail:^(NSURLSessionDataTask *task, NSError *error) {
        [self showToast:Failed];
    }];
    
}

#pragma make ---------->>头部界面创建
- (void)setupCreat{
    UIView * backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, NavHeight, ScreenWidth, 290 * px);
    backView.backgroundColor = wycMain;
    [self.view addSubview:backView];
    
    
    UIView * backView2 = [[UIView alloc] init];
    backView2.frame = CGRectMake(0, CGRectGetMaxY(backView.frame), ScreenWidth, 320 * px);
    backView2.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView2];
    
    
    UIView * backView1 = [[UIView alloc] init];
    backView1.frame = CGRectMake(40 * px, 0, ScreenWidth - 80 * px, 310 * px);
    backView1.layer.cornerRadius = 30 * px;
    backView1.backgroundColor = [UIColor whiteColor];
    [backView addSubview:backView1];
    
    UIView * backView3 = [[UIView alloc] init];
    backView3.frame = CGRectMake(40 * px, 0, ScreenWidth - 80 * px, 290 * px);
    //    backView3.layer.cornerRadius = 30 * px;
    backView3.backgroundColor = [UIColor whiteColor];
    backView3.layer.shadowColor = [UIColor blackColor].CGColor;//设置阴影的颜色
    
    backView3.layer.shadowOpacity = 0.4;//设置阴影的透明度
    
    backView3.layer.shadowOffset = CGSizeMake(1, 1);//设置阴影的偏移量
    
    backView3.layer.shadowRadius = 3;//设置阴影的圆角
    
    [backView2 addSubview:backView3];
    
    
    
    
    
    UILabel * balancelabel = [[UILabel alloc] init];
    balancelabel.frame = CGRectMake(backView1.width / 2 - 300 * px , 100 * px, 600  * px, 70 * px);
    NSString * billAmount = [NSString stringWithFormat:@"%.2f",[self.billAmount doubleValue]];
    balancelabel.text = billAmount;
    balancelabel.textColor = [UIColor blackColor];
    balancelabel.textAlignment = NSTextAlignmentCenter;
    balancelabel.font = [UIFont systemFontOfSize:BigFont];
    [backView1 addSubview:balancelabel];
    
    UILabel *benqizhangdan = [[UILabel alloc] init];
    benqizhangdan.frame = CGRectMake(balancelabel.x, CGRectGetMaxY(balancelabel.frame) + 20 * px, balancelabel.width, 50 * px);
    benqizhangdan.text = @"本期账单(元)";
    benqizhangdan.textAlignment = NSTextAlignmentCenter;
    benqizhangdan.textColor = WYCRGBColor(167, 175, 179);
    benqizhangdan.font = [UIFont systemFontOfSize:SmallFont];
    [backView1 addSubview:benqizhangdan];
    
    self.typelabel = [[UILabel alloc] init];
    
    
    WYCLog(@"-================%@",self.state);
    self.typelabel = [[UILabel alloc] init];
    if ([self.state isEqualToString:@"0"]){
        
        self.typelabel.text = @"";
    } else if ([self.state isEqualToString:@"1"]){
        NSString * aooroveStr = [NSString stringWithFormat:@"待还款"];
        CGSize stateSize = [aooroveStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:40 * px]}];
        self.typelabel.text = aooroveStr;
        self.typelabel.textColor = WYCMain;
        self.typelabel.textAlignment = NSTextAlignmentCenter;
        self.typelabel.backgroundColor = [UIColor whiteColor];
        self.typelabel.frame = CGRectMake(backView.width - stateSize.width - 110 * px, 40 * px, stateSize.width + 60 * px, stateSize.height);
        UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.typelabel.layer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20 * px, 20 * px)];
        CAShapeLayer * maskLayer = [CAShapeLayer new];
        maskLayer.frame = self.typelabel.layer.bounds;
        maskLayer.path = maskPath.CGPath;
        self.typelabel.layer.mask = maskLayer;
        [backView addSubview:self.typelabel];
    }else if ([self.state isEqualToString:@"2"]){
        NSString * aooroveStr = [NSString stringWithFormat:@"已还清"];
        CGSize stateSize = [aooroveStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:40 * px]}];
        self.typelabel.text = aooroveStr;
        self.typelabel.textAlignment = NSTextAlignmentCenter;
        self.typelabel.backgroundColor = [UIColor greenColor];
        self.typelabel.frame = CGRectMake(backView.width - stateSize.width - 110 * px, 40 * px, stateSize.width + 60 * px, stateSize.height);
        UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.typelabel.layer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20 * px, 20 * px)];
        CAShapeLayer * maskLayer = [CAShapeLayer new];
        maskLayer.frame = self.typelabel.layer.bounds;
        maskLayer.path = maskPath.CGPath;
        self.typelabel.layer.mask = maskLayer;
        [backView addSubview:self.typelabel];
    }else if ([self.state isEqualToString:@"3"]){
        NSString * aooroveStr = [NSString stringWithFormat:@"智能还款中"];
        self.typelabel.text = aooroveStr;
        CGSize stateSize = [aooroveStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:40 * px]}];
        self.typelabel.textAlignment = NSTextAlignmentCenter;
        self.typelabel.backgroundColor = [UIColor blueColor];
        self.typelabel.frame = CGRectMake(backView.width - stateSize.width - 110 * px, 40 * px, stateSize.width + 60 * px, stateSize.height);
        UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.typelabel.layer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20 * px, 20 * px)];
        CAShapeLayer * maskLayer = [CAShapeLayer new];
        maskLayer.frame = self.typelabel.layer.bounds;
        maskLayer.path = maskPath.CGPath;
        self.typelabel.layer.mask = maskLayer;
        [backView addSubview:self.typelabel];
    }else if ([self.state isEqualToString:@"4"])
    {
        
        NSString * aooroveStr = [NSString stringWithFormat:@"智能还款终止"];
        CGSize stateSize = [aooroveStr sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:40 * px]}];
        self.typelabel.text = aooroveStr;
        self.typelabel.textAlignment = NSTextAlignmentCenter;
        self.typelabel.backgroundColor = [UIColor redColor];
        self.typelabel.frame = CGRectMake(backView.width - stateSize.width - 110 * px, 40 * px, stateSize.width + 30 * px, stateSize.height);
        UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:self.typelabel.layer.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(20 * px, 20 * px)];
        CAShapeLayer * maskLayer = [CAShapeLayer new];
        maskLayer.frame = self.typelabel.layer.bounds;
        maskLayer.path = maskPath.CGPath;
        self.typelabel.layer.mask = maskLayer;
        [backView addSubview:self.typelabel];
    }

    
    
    //账单日
    UILabel *Statementlabel = [[UILabel alloc] init];
    Statementlabel.frame = CGRectMake(0, 80 * px, backView3.width / 4, 50 * px);
    Statementlabel.text = @"账单日";
    Statementlabel.textAlignment = NSTextAlignmentCenter;
    Statementlabel.textColor = [UIColor blackColor];
    Statementlabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView3 addSubview:Statementlabel];
    
    UILabel *Statement = [[UILabel alloc] init];
    Statement.frame = CGRectMake(0, Statementlabel.frame.size.height+Statementlabel.frame.origin.y,Statementlabel.width , 50*px);
    Statement.text = self.billDate;
    Statement.textAlignment = NSTextAlignmentCenter;
    Statement.textColor = [UIColor blackColor];
    Statement.font = [UIFont systemFontOfSize:SmallFont];
    [backView3 addSubview:Statement];
    
    
    
    UILabel *Repaymentlabel = [[UILabel alloc] init];
    Repaymentlabel.frame = CGRectMake(backView3.width / 4, Statementlabel.y, Statementlabel.width, Statementlabel.height);
    Repaymentlabel.text = @"还款日";
    Repaymentlabel.textAlignment = NSTextAlignmentCenter;
    Repaymentlabel.textColor = [UIColor blackColor];
    Repaymentlabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView3 addSubview:Repaymentlabel];
    
    UILabel *Repayment = [[UILabel alloc] init];
    Repayment.frame = CGRectMake(backView3.width / 4, Statement.y, Statementlabel.width, Statementlabel.height);
    Repayment.text  =self.dueDate;
    Repayment.textColor = [UIColor blackColor];
    Repayment.textAlignment = NSTextAlignmentCenter;
    Repayment.font = [UIFont systemFontOfSize:SmallFont];
    [backView3 addSubview:Repayment];
    
    
    
    //免息期
    UILabel *Gracelabel = [[UILabel alloc] init];
    Gracelabel.frame = CGRectMake(backView3.width / 2, Statementlabel.y, Statementlabel.width, Statementlabel.height);
    Gracelabel.text = @"免息期";
    Gracelabel.textAlignment = NSTextAlignmentCenter;
    Gracelabel.textColor = [UIColor blackColor];
    Gracelabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView3 addSubview:Gracelabel];
    
    UILabel *Grace = [[UILabel alloc] init];
    Grace.frame = CGRectMake(backView3.width / 2, Statement.y, Statementlabel.width, Statementlabel.height);
    NSString * gracePeriod = [NSString stringWithFormat:@"%@",self.gracePeriod];
    Grace.text = gracePeriod;
    Grace.textColor = [UIColor blackColor];
    Grace.textAlignment = NSTextAlignmentCenter;
    Grace.font = [UIFont systemFontOfSize:SmallFont];
    [backView3 addSubview:Grace];
    
    
    
    //总额度
    UILabel *Totallabel = [[UILabel alloc] init];
    Totallabel.frame = CGRectMake(backView3.width / 4 * 3, Statementlabel.y, Statementlabel.width, Statementlabel.height);
    Totallabel.text = @"总额度";
    Totallabel.textAlignment = NSTextAlignmentCenter;
    Totallabel.textColor = [UIColor blackColor];
    Totallabel.font = [UIFont systemFontOfSize:SmallFont];
    [backView3 addSubview:Totallabel];
    
    
    
    UILabel *Total = [[UILabel alloc] init];
    Total.frame = CGRectMake(backView3.width / 4 * 3, Statement.y, Statementlabel.width, Statementlabel.height);
    Total.textAlignment = NSTextAlignmentCenter;
    NSString * creditLine = [NSString stringWithFormat:@"%.2f",[self.creditLine doubleValue]];
    Total.text = creditLine;
    Total.textColor = [UIColor blackColor];
    Total.font = [UIFont systemFontOfSize:SmallFont];
    [backView3 addSubview:Total];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(backView2.frame), ScreenWidth,  180 * 5 * px) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    
    self.tableView.delegate = self;
    self.tableView.scrollEnabled = NO;
    self.tableView.tableFooterView.height = 120 * px;
    [self.view addSubview:self.tableView];
    
    
    backLabel = [[UILabel alloc] init];
    backLabel.textAlignment = NSTextAlignmentLeft;
    //    backLabel.backgroundColor = [UIColor redColor];
    backLabel.text = @"手续费=还款总金额×0.69%+还款笔数×1元";
    backLabel.font = [UIFont systemFontOfSize:SmallFont];
    backLabel.textColor = [UIColor redColor];
    backLabel.frame = CGRectMake(40 * px, CGRectGetMaxY(self.tableView.frame) + 80 * px, ScreenWidth - 80 * px, 48 * px);
    [self.view addSubview:backLabel];
    
    UIButton * returnButton = [[UIButton alloc] init];
    returnButton.frame = CGRectMake( 40 * px, CGRectGetMaxY(backLabel.frame) + 40 * px, ScreenWidth - 80 * px, 150 * px);
    returnButton.layer.cornerRadius = 75 * px;
    returnButton.backgroundColor = wycMain;
    [returnButton setTitle:@"生成智能还款计划" forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(returnButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [returnButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:returnButton];
    
    
    
    
    
    
}

#pragma make ----------->>Nav
- (void)setupNav{
    //    [self.navigationController.navigationBar setBarTintColor:WYCmainColor];
    //    [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    
    //    self.navigationItem.title = [NSString stringWithFormat:@"%@(%@)",self.bankName,self.lastNumber];
    
}

#pragma ----------->数据元代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 5;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 180 * px;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    tableView.tableFooterView = [[UIView alloc] init];
    
    WYCTRepayCell * cell = [WYCTRepayCell cellWithTableView:tableView];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.repaymodel = self.repayarray[indexPath.row];
    if (indexPath.row == 4 || indexPath.row == 3) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    }else{
        
    }
    
    
    [cell.addButton addTarget:self action:@selector(addButtonClick) forControlEvents:UIControlEventTouchUpInside];
    //    [cell.riliButton addTarget:self action:@selector(CalculatorButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [cell.rustButton addTarget:self action:@selector(reduceButtonClick) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
#pragma make --------->>日历选择
- (void)CalculatorButtonClick{
    WYCLogFunc;
    if (!_calendarView) {
        _calendarView = [[LDCalendarView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH,SCREEN_HEIGHT)];
        [self.view addSubview:_calendarView];
        
        __weak typeof(self) weakSelf = self;
        _calendarView.complete = ^(NSArray *result) {
            if (result.count != 0) {
                weakSelf.seletedDays = [result mutableCopy];
                WYCRepayModel * model3 = weakSelf.repayarray[3];
                NSString * str = [NSString stringWithFormat:@"%@",weakSelf.showStr];
                NSString *b = [str substringToIndex:[str length]-1];
                model3.coment = b;
                [weakSelf.tableView reloadData];
            }
        };
    }
    [self.calendarView show];
    
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WYCRepayModel *model1 = self.repayarray[0];
    
    WYCRepayModel *model2 = self.repayarray[1];
    
    WYCRepayModel * model3 = self.repayarray[2];
    
    WYCRepayModel * model4 = self.repayarray[3];
    
    
    int balanceintValue = [model2.coment intValue];
    
    int huankuan = [model1.coment intValue];
    
    int onestr = huankuan * 0.06;
    
    int str = balanceintValue;
    
    
    
    
    if (indexPath.row == 3) {
        if (model1.coment.length == 0) {
            [self showToast:@"还款金额不能为空"];
            return;
        }
        
        
        
        if (model2.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"卡内余额不能为空"];
            return;
        }
        if (model3.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款天数不能为空"];
            return;
        }
        if (str < onestr) {
            
            [self showToast:@"余额不能低于还款金额的6%"];
            return;
        }else{
            [self CalculatorButtonClick];
        }
        
    }
    
    if (indexPath.row == 4) {
        WYCLogFunc;
        if (model1.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款金额不能为空"];
            return;
        }
        
        
        
        if (model2.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"卡内余额不能为空"];
            return;
        }
        if (model3.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款天数不能为空"];
            return;
        }
        
        if (model4.coment.length == 0) {
            [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"消费日期不能为空"];
            return;
        }
        
        WYCSelectionPlanController * SetUsercontroller = [[WYCSelectionPlanController alloc] init];
        SetUsercontroller.bankId = self.seeBillModel.bankId;
        SetUsercontroller.Delegate = self;
        
        int repaymentintValue = [model1.coment intValue];
        
        int repaymentintValue2 = [model3.coment intValue];
        
        int str = repaymentintValue / repaymentintValue2;
        SetUsercontroller.dailyAmount = [NSString stringWithFormat:@"%d",str];
        
        SetUsercontroller.balance = [NSString stringWithFormat:@"%d",repaymentintValue];
        
        [self.navigationController pushViewController:SetUsercontroller animated:NO];
        
        
        // 取消选中状态
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
    
}
#pragma make --------->>加号按钮
- (void)addButtonClick{
    WYCLogFunc;
    WYCRepayModel * model3 =self.repayarray[2];
    summer = [model3.coment intValue];
    summer++;
    model3.coment = [NSString stringWithFormat:@"%d",summer];
    
    [self.tableView reloadData];
    
}

#pragma make --------->>减号按钮
- (void)reduceButtonClick{
    WYCLogFunc;
    WYCRepayModel * model3 =self.repayarray[2];
    summer = [model3.coment intValue];
    if (summer == 0) {
        return;
    }
    summer--;
    model3.coment = [NSString stringWithFormat:@"%d",summer];
    
    [self.tableView reloadData];
    
}
- (void)returnButtonClick{
    WYCLogFunc;
    WYCRepayModel *model0 = self.repayarray[0];
    
    WYCRepayModel *model1 = self.repayarray[1];
    
    WYCRepayModel * model2 = self.repayarray[2];
    
    WYCRepayModel * model3 = self.repayarray[3];
    
    WYCRepayModel * model4 = self.repayarray[3];
    
    if (model1.coment.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款金额不能为空"];
        return;
    }
    
    
    
    if (model2.coment.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"卡内余额不能为空"];
        return;
    }
    if (model3.coment.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"还款天数不能为空"];
        return;
    }
    
    if (model4.coment.length == 0) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"消费日期不能为空"];
        return;
    }
    
    
    
    NSString * str = [NSString stringWithFormat:@"%@",self.showStr];
    NSArray * arr = [str componentsSeparatedByString:@","];
    WYCLog(@"还款日期%@",self.showStr);
    WYCLog(@"arr%lu",(unsigned long)arr.count);
    NSString *timedays = [NSString stringWithFormat:@"%lu",(long)arr.count];
    
    NSString *days = [NSString stringWithFormat:@"%d",summer];
    WYCLog(@"还款天数%d",summer);
    NSString *b = [str substringToIndex:[str length]-1];
    model3.coment = b;
    if ([timedays intValue] - 1 != [days intValue]) {
        [self showToast:@"消费日期与选择日期天数不匹配"];
        return;
    }
    WYCLog(@"还款天数%lu",(unsigned long)arr.count);
    
    
    NSMutableDictionary * params = [NSMutableDictionary dictionary];
    params[@"token"] = [WYCAccountTool unarchivetoken];
    params[@"cardId"] = self.seeBillModel.cardId;
    params[@"amount"] = model0.coment;
    params[@"balance"] = model1.coment;
    params[@"days"] = model2.coment;
    params[@"consumeDates"] = model3.coment;
    params[@"consumePlanId"] = self.planID;
    params[@"repaymentType"] = @"1";
    
    WYCLog(@"--------%@",self.state);
    
    if ([self.state isEqualToString:@"1"] || [self.state isEqualToString:@"3"]) {
        [SVProgressHUD showImage:[UIImage imageNamed:@""] status:@"已存在的待执行的计划"];
        return;
    }else{
        WYCPreviewController * preview = [[WYCPreviewController alloc] init];
        preview.parmDic = params;
        [self.navigationController pushViewController:preview animated:NO];
    }
    
    
    
    
}
- (void)setDelegatemaxNumber:(NSString *)maxNumber setID:(NSString *)ID{
    WYCRepayModel * model4= self.repayarray[4];
    model4.coment = [NSString stringWithFormat:@"%@",maxNumber];
    
    self.planID = ID;
    [self.tableView reloadData];
}
#pragma make ---------->>进入控制器改变Nav颜色
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    
    
    UIView * NavView = [[UIView alloc] init];
    NavView.frame = CGRectMake(0, 0, ScreenWidth, NavHeight);
    NavView.backgroundColor = WYCmainColor;
    [self.view addSubview:NavView];
    
    
    
    self.bottomLineLayer = [[CALayer alloc] init];
    self.bottomLineLayer.frame = CGRectMake(0, NavView.bounds.size.height - 0.5, NavView.bounds.size.width, 0.5);
    self.bottomLineLayer.backgroundColor = WYCRGBColor(191, 191, 196).CGColor;
    //    [NavView.layer addSublayer:self.bottomLineLayer];
    
    
    //返回按钮
    UIButton *  backbutton = [[UIButton alloc] init];
    [backbutton setImage:[UIImage imageNamed:@"Nav_back"] forState:UIControlStateNormal];
    [backbutton addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    backbutton.frame = CGRectMake(30 * px, titleY, 66 * px , 66 * px);
    [NavView addSubview:backbutton];
    
    
    UILabel * NavRigsterLabel = [[UILabel alloc] init];
    NavRigsterLabel.frame = CGRectMake(0, titleY, NavView.width, 68 * px);
    NavRigsterLabel.textColor = [UIColor whiteColor];
    NavRigsterLabel.font = [UIFont boldSystemFontOfSize:BigFont];
    NavRigsterLabel.textAlignment = NSTextAlignmentCenter;
    NavRigsterLabel.text = [NSString stringWithFormat:@"%@(%@)",self.bankName,self.lastNumber];
    [NavView addSubview:NavRigsterLabel];
    
    
}
- (void)backClick{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)dealloc {
    
    [self.repayModel2 removeObserver:self forKeyPath:@"coment"];
}
@end

